package com.bhs.sansong.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.LogisticsDiscountCouponAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.CouponSearchBean;
import com.bhs.sansong.bean.DiscountCoupon;
import com.bhs.sansong.bean.DiscountCouponBean;
import com.bhs.sansong.bean.MenuBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.discountCoupon.CouponSearchActivity;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 优惠券弹窗
 */
public class BottomCouponDialog extends Dialog implements View.OnClickListener, NetCallBack {
    private RecyclerView mRvList;
    private List<DiscountCouponBean> list;
    private LogisticsDiscountCouponAdapter mAdapter;
    private FrameLayout mFlClose;
    private int page = 1;
    private BaseFragment baseFragment;
    private int logistics_id;

    public BottomCouponDialog(@NonNull BaseFragment baseFragment, List<DiscountCouponBean> list, int logistics_id) {
        super(baseFragment.getContext(), R.style.MyBottomDialogStyle);
        this.list = list;
        this.baseFragment = baseFragment;
        this.logistics_id = logistics_id;
    }

    public BottomCouponDialog(@NonNull BaseActivity activity, List<DiscountCouponBean> list) {
        super(activity, R.style.MyBottomDialogStyle);
        this.list = list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_coupon);
        //获取当前Activity所在的窗体
        Window dialogWindow = getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置动画
        dialogWindow.setWindowAnimations(R.style.main_menu_animstyle);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp); //将属性设置给窗体
        initView();
    }

    private void initView() {
        mRvList = findViewById(R.id.rv_list);
        mFlClose = findViewById(R.id.fl_close);
        mFlClose.setOnClickListener(this);
        mAdapter = new LogisticsDiscountCouponAdapter(list);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        if (baseFragment != null) {
            mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                @Override
                public void onLoadMoreRequested() {
                    page++;
                    loadData();
                }
            }, mRvList);
        }

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(baseFragment!=null) {
                    if (mAdapter.getData().get(position).getCan_take() == 1) {
                        baseFragment.networkRequest(baseFragment.netApi.takeCoupon(mAdapter.getData().get(position).getId()), BottomCouponDialog.this, 555);
                        mAdapter.getData().get(position).setCan_take(0);
                        mAdapter.notifyItemChanged(position);
                    }
                }else {
                    dismiss();
                    listener.onClick(String.valueOf(mAdapter.getData().get(position).getId()),mAdapter.getData().get(position).getCoupon_price());
                }
            }
        });
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty2, null));

    }

    private void loadData() {
        baseFragment.networkRequest(baseFragment.netApi.companyCouponList(page, 10, logistics_id), this, 666);
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 666) {
            CouponSearchBean bean = new Gson().fromJson(json, CouponSearchBean.class);
            if (bean.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(bean.getData().getList());
                    if (mAdapter.getData().size() > 0) {
                        mAdapter.notifyDataSetChanged();
                    }
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(bean.getData().getList());
                }
                if (MyUtils.isEmpty(bean.getData().getList())) {
                    mAdapter.loadMoreEnd();
                }

            } else {
                ToastUtil.show(bean.getMsg());
            }
        } else if (requestCode == 555) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            if (bean.getCode().equals("1")) {
                ToastUtil.show("领取成功");
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mAdapter.loadMoreEnd();
    }

    private MyClickListener2 listener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.listener = l;
    }
}
